<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Tymon\JWTAuth\Contracts\JWTSubject;
use App\Service\Model\Cards;
use App\Service\Model\Addcus;
use App\Service\Facades\Pay;
use App\Service\Model\Orders;
use Encore\Admin\Traits\ModelTree;
use Encore\Admin\Traits\AdminBuilder;

/**
 * @property \Illuminate\Database\Eloquent\Collection $cards
 * @property \Illuminate\Database\Eloquent\Collection $addcus
 * @property \Illuminate\Database\Eloquent\Collection $orders
 */
class User extends Authenticatable implements JWTSubject
{
    use ModelTree,AdminBuilder;
    const user_type_normal          = 0;//普通用户
    const user_type_first_agent     = 1;//一级代理
    const user_type_secondary_agent = 2;//二级代理

    const user_type_text = [
        self::user_type_normal => '普通用户',
        self::user_type_first_agent => '一级代理',
        self::user_type_secondary_agent => '二级代理',
    ];


    const status_default  = 1; //未认证
    const status_verified = 2; //已认证
    const status_bind     = 3; //已绑卡

    use Notifiable;
    const attribute = [
        'tel'       => '手机号',
        'password'  => '密码',
        'sms_code'  => '短信验证码',
        'key'       => '验证码KEY',
        'action'    => '类型',
    ];
    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);
        $this->setParentColumn('parent_id');
        //$this->setOrderColumn('sort');
        $this->setTitleColumn('name');
    }
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'tel','password','cusid','parent_id','agency_level','agency_id','agency_name'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];


    public function getJWTCustomClaims()
    {
        return [];
    }

    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    /**
     * 通过手机号找到用户
     * @param string $tel
     * @return Collection
     */
    public static function findUserByTel($tel){
        return self::where(['tel'=>$tel])->first();
    }

    /**
     * 获取用户
     *
     * @param string $invite_code
     * @return void
     */
    public static function getUserByInviteCode($invite_code){
        return self::where('invite_code',$invite_code)->first();
    }
    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     * 获取用户已绑定的卡
     */
    public function cards()
    {
        return $this->hasMany(Cards::class,'uid')->where('is_delete',0);
    }

    /**
     * 获取进件信息
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function addcus(){
        return $this->hasOne(Addcus::class,'uid');
    }


    /**
     * Undocumented function
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function orders(){
        return $this->hasMany(Orders::class,'uid');
    }

    /**
     * 查询用户余额
     * 
     * @return boolean|int
     */
    public function queryBalance(){
        $ret = Pay::queryBalance($this->cusid);
        if(Pay::check($ret)){
            $this->balance = $ret->balance;
            $this->save();
            return $ret->balance;
        }
        return false;
    }

    /**
     * 找到自己的下级
     *
     * @return void
     */
    public function nextAgent(){
        return $this->hasMany(self::class,'parent_id');
    }
    
}
